<?php



//header('Refresh: 1; URL=http://www.allpowershop.com.au');
require_once('lib/swift_required.php');
include 'connection.php';
include 'warrantypdf.php';

$Storename = $_POST['storename'];

$Saleperson = $_POST['saleperson'];

$Brand = $_POST['brand'];

$Model = $_POST['model'];

$Serialnumber = $_POST['serialnumber'];

$Usage = $_POST['usage'];

$Purchasedate = $_POST['date'];

$Invoicenumber = $_POST['invoicenumber'];

$Price = $_POST['price'];

$FirstName = $_POST['firstname'];

$Surname = $_POST['surname'];

$Contractornumber = $_POST['contractornumber'];

$Contractortype = $_POST['contractortype'];

$Businessname = $_POST['businessname'];

$Address = $_POST['address'];

$Suburb = $_POST['suburb'];

$Postcode = $_POST['postcode'];

$State = $_POST['state'];

$Phone = $_POST['phone'];

$Email = $_POST['email'];


$query = "SELECT COUNT(*) from warranties WHERE serialnumber='".$Serialnumber."'";

$results = mysql_query($query);

$result = mysql_fetch_row($results);

$availability = $result[0];

if ($availability != 0)

{

	//serial number is not availble

	die ("You entered a serial number that has already been registered. Your Serial Number must be unique. Please use the back button and check your entries on the form.");

} 
$query= "INSERT INTO `warranties`(`storename`, `salesperson`, `brand`, `model`, `serialnumber`, `usage`, `purchasedate`, `invoicenumber`, `purchaseprice`, `firstname`, `lastname`, `contractornumber`, `contractortype`, `businessname`, `address`, `suburb`, `postcode`, `state`, `phone`, `email`) VALUES ('".$Storename."','".$Saleperson."','".$Brand."','".$Model."','".$Serialnumber."','".$Usage."','".$Purchasedate."','".$Invoicenumber."','".$Price."','".$FirstName."','".$Surname."','".$Contractornumber."','".$Contractortype."','".$Businessname."','".$Address."','".$Suburb."','".$Postcode."','".$State."','".$Phone."','".$Email."')";

mysql_query($query) or die ("The system has encountered an error. Please use the back button to return to your form and try submit again.");

	$EmailSubject = 'Extended Warranty';

	$mailheader = "From: info@allpower.com.au \r\n";  

	$mailheader .= "Content-type: text/html; charset=iso-8859-1 \r\n";

	$mailheader .= "To: kevin@techheadsolutions.com.au  \r\n";

	$MESSAGE_BODY = "A new Warranty Registration has been made with the following details:<br /><br />

	First Name: $FirstName<br />

    Last Name: $Surname<br />

    Contractor Number: $Contractornumber<br />

	Contractor Type: $Contractortype<br />

	Business Name: $Businessname<br />

	Street Address: $Address<br />

	Suburb: $Suburb<br />

	Postcode: $Postcode<br />

	State: $State<br />

	Phone: $Phone<br />

	Email: $Email<br />

	Store Name: $Storename<br />

	Sales Person: $Saleperson<br />

	Brand: $Brand<br />

	Model: $Model<br />

	Serial Number: $Serialnumber<br />

	Usage: $Usage<br />

	Purchase Date: $Purchasedate<br />

	Invoice Number: $Invoicenumber<br />

	Price: $Price<br />";

    mail("", $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("There was an error sending the mail");

	createPdf($Storename, $Saleperson, $Brand, $Model, $Serialnumber, $Usage, $Purchasedate, $Invoicenumber, $Price, $FirstName, $Surname, $Contractortype, $Contractornumber, $Businessname, $Address, $Suburb, $Postcode, $State, $Phone, $Email);
	
	$filename = 'warranty_'.$serialnumber.'.pdf';
	?>
	<script type="text/javascript">
	var filename = "<?php echo $filename; ?>";
	window.open (filename)</script>
	
	<?php
	
	if (strtoupper(substr(PHP_OS,0,3)=='WIN')) { 
  $eol="\r\n"; 
} elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')) { 
  $eol="\r"; 
} else { 
  $eol="\n"; 
} ?> 

<?php 
if (isset ($Email) && $Email != ""){
# File for Attachment 
$f_name=$filename;    // use relative path OR ELSE big headaches. $letter is my file for attaching. 
$handle=fopen($f_name, 'rb'); 
$f_contents=fread($handle, filesize($f_name)); 
$f_contents=chunk_split(base64_encode($f_contents));    //Encode The Data For Transition using base64_encode(); 
$f_type=filetype($f_name); 
fclose($handle); 
# To Email Address 
$emailaddress=$Email; 
# Message Subject 
$emailsubject="Allpower Warranty Certificate"; 
# Message Body 
$body="Please find attached a copy of your warranty certificate"; 

# Common Headers 
$headers .= 'From: Allpower <info@allpower.com.au>'.$eol; 
$headers .= 'Reply-To: Allpower <info@allpower.com.au>'.$eol; 
$headers .= 'Return-Path: Allpower <info@allpower.com.au>'.$eol;     // these two to set reply address 
//$headers .= "Message-ID:<".$now." TheSystem@".$_SERVER['SERVER_NAME'].">".$eol; 
//$headers .= "X-Mailer: PHP v".phpversion().$eol;           // These two to help avoid spam-filters 
# Boundry for marking the split & Multitype Headers 
$mime_boundary=md5(time()); 
$headers .= 'MIME-Version: 1.0'.$eol; 
$headers .= "Content-Type: multipart/related; boundary=\"".$mime_boundary."\"".$eol; 
$msg = ""; 

# Attachment 
$msg .= "--".$mime_boundary.$eol; 
$msg .= "Content-Type: application/pdf; name=\"".$filename."\"".$eol;   // sometimes i have to send MS Word, use 'msword' instead of 'pdf' 
$msg .= "Content-Transfer-Encoding: base64".$eol; 
$msg .= "Content-Disposition: attachment; filename=\"".$filename."\"".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !! 
$msg .= $f_contents.$eol.$eol;  

# HTML Version 
$msg .= "--".$mime_boundary.$eol; 
$msg .= "Content-Type: text/html; charset=iso-8859-1".$eol; 
$msg .= "Content-Transfer-Encoding: 8bit".$eol; 
$msg .= $body.$eol.$eol; 

# Finished 
$msg .= "--".$mime_boundary."--".$eol.$eol;   // finish with two eol's for better security. see Injection. 

# SEND THE EMAIL  
mail($emailaddress, $emailsubject, $msg, $headers)  or die ("There was an error sending the second mail"); 
}

		
	echo '<script type="text/javascript">

	alert("Thank you for registering,  your details have been forwarded to our Customer Services Team who will forward your Warranty confirmation.")

	</script>';
	
	$pdffile = $_SERVER['DOCUMENT_ROOT'].'/tcpdf/warranty_certificate_'.$FirstName.'_'.$Surname.'.pdf';
	$open = fopen($pdffile, 'r');
	$warranty_certificate = 'warranty_certificate_'.$FirstName.'_'.$Surname.'.pdf';
	

if (!isset ($_POST['addanother']))

{

printf("<script>location.href='http://www.allpowershop.com.au'</script>");

}

else 

{

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link rel="stylesheet" type="text/css" href="styles.css" />

<title>AllPower - The Service Experience</title>

<style type="text/css">

#title2 {

	font-size: 16px;

	color: #FFF;

	font-weight: bold;

}

.title1 {

	font-size: 18pt;

	color: #000;

        font-face: Arial;

	font-weight: bold;

}

.subtitle {

	font-size: 11pt;

	color: #000;

        font-face: Arial;

	font-weight: bold;

}

.normaltext {

	font-size: 11pt;

	color: #000;

        font-face: Arial;

}

</style>

<script language="JavaScript">

  function VerifyDataEntry(entry)

  {

    if(entry.storename.value == "")

    {

      alert("Please enter a value for the \"storename\" field");

      entry.storename.focus();

      return false;

    }



    if(entry.brand.value == "")

    {

      alert("Please enter a value for the \"brand\" field");

      entry.brand.focus();

      return false;

    }



    if(entry.model.value == "")

    {

      alert("Please enter a value for the \"model\" field");

      entry.model.focus();

      return false;

    }



    if(entry.serialnumber.value == "")

    {

      alert("Please enter a value for the \"serialnumber\" field");

      entry.serialnumber.focus();

      return false;

    }

	

	if(entry.usage.value == "")

    {

      alert("Please enter a value for the \"usage\" field");

      entry.usage.focus();

      return false;

    }

	

	if(entry.date.value == "")

    {

      alert("Please enter a value for the \"Purchase Date\" field");

      entry.date.focus();

      return false;

    }

	

	if(entry.invoicenumber.value == "")

    {

      alert("Please enter a value for the \"Invoice Number\" field");

      entry.invoicenumber.focus();

      return false;

    }

	

	if(entry.price.value == "")

    {

      alert("Please enter a value for the \"price\" field");

      entry.price.focus();

      return false;

    }

	

	if(entry.firstname.value == "")

    {

      alert("Please enter a value for the \"first name\" field");

      entry.firstname.focus();

      return false;

    }

	

	if(entry.surname.value == "")

    {

      alert("Please enter a value for the \"last name\" field");

      entry.surname.focus();

      return false;

    }

	

	if(entry.address.value == "")

    {

      alert("Please enter a value for the \"address\" field");

      entry.address.focus();

      return false;

    }

	

	if(entry.suburb.value == "")

    {

      alert("Please enter a value for the \"suburb\" field");

      entry.suburb.focus();

      return false;

    }

	

	if(entry.postcode.value == "")

    {

      alert("Please enter a value for the \"postcode\" field");

      entry.postcode.focus();

      return false;

    }

	

	if(entry.state.value == "")

    {

      alert("Please enter a value for the \"state\" field");

      entry.state.focus();

      return false;

    }

	

	if(entry.phone.value == "")

    {

      alert("Please enter a value for the \"phone\" field");

      entry.phone.focus();

      return false;

    }

	

	if(entry.email.value == "")

    {

      alert("Please enter a value for the \"email\" field");

      entry.email.focus();

      return false;

    }

	var x=document.forms["entry"]["email"].value;

    var atpos=x.indexOf("@");

    var dotpos=x.lastIndexOf(".");

    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)

      {

        alert("Not a valid e-mail address");

        return false;

      }

    return true;

  }

</script>

<script src="jquery.js" type="text/javascript" language="javascript"></script>

<script language="javascript">

//<!---------------------------------+

//  Developed by Roshan Bhattarai 

//  Visit http://roshanbh.com.np for this script and more.

//  This notice MUST stay intact for legal use

// --------------------------------->

$(document).ready(function()

{

	$("#serialnumber").blur(function()

	{

		//remove all the class add the messagebox classes and start fading

		$("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");

		//check the username exists or not from ajax

		$.post("availability.php",{ serial_number:$(this).val() } ,function(data)

        {

		  if(data=='no') //if username not avaiable

		  {

		  	$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox

			{ 

			  //add message and change the class of the box and start fading

			  $(this).html('This Serial Number already exists').addClass('messageboxerror').fadeTo(900,1);

			});		

          }

		  else

		  {

		  	$("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox

			{ 

			  //add message and change the class of the box and start fading

			  $(this).html('Serial Number available to register').addClass('messageboxok').fadeTo(900,1);	

			});

		  }

				

        });

 

	});

});

</script>

<style type="text/css">

body {

font-family:Verdana, Arial, Helvetica, sans-serif;

font-size:11px;

}

.top {

margin-bottom: 50px;

}

.messagebox{

	position:relative;

	width:100px;

	margin-left:20px;

	margin-bottom: 50px;

	border:1px solid #c93;

	background:#ffc;

	padding:3px;

}

.messageboxok{

	position:relative;

	width:auto;

	margin-left:20px;

	margin-bottom: 50px;

	border:1px solid #349534;

	background:#C9FFCA;

	padding:3px;

	font-weight:bold;

	color:#008000;

	

}

.messageboxerror{

	position:relative;

	width:auto;

	margin-left:20px;

	margin-bottom: 50px;

	border:1px solid #CC0000;

	background:#F7CBCA;

	padding:3px;

	font-weight:bold;

	color:#CC0000;

}



</style>

</head>

<body>

<div id="content">

<div id="header">

<br />

<br />

<div class="image" style="padding-bottom:50px;">

<img src="images/header_logo.png" style="padding-left:30px;" border="0" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="images/top_buttons_all.png" border="0" usemap="#Map" />

<map name="Map" id="Map">

  <area shape="poly" coords="0,4,107,4,108,78,1,67" href="http://allpowershop.com.au/shop/home.php" alt="shop online" />

  <area shape="poly" coords="136,5,244,4,244,79,135,67" href="http://allpowershop.com.au/shop/home.php?cat=16" alt="Warehouse Clearance" />

  <area shape="poly" coords="271,4,378,4,379,80,271,67" href="http://allpowershop.com.au/shop/store_locator.php" alt="Find a Dealer" />

  <area shape="poly" coords="406,4,513,4,514,80,406,68" href="http://allpowershop.com.au/shop/pages.php?pageid=13" alt="Warranty Registration" />

  <area shape="poly" coords="539,4,646,4,647,79,540,66" href="http://allpowershop.com.au/shop/home.php" alt="Products" />

  <area shape="poly" coords="674,4,779,4,778,81,674,67" href="#" alt="Current Promotions" onClick="alert('This page is currently under construction. Please check again soon');return false" />

</map>

</div>



<div class="horizontalbreak">&nbsp;</div>

<div id="main">

<br />

<br />

<div id="maintext2">

<center><img src="images/warrantyheader.png" width="800px" /></center>

<div id="form" style="padding-left:320px;">

Please note: Items marked with an asterisk are required.

<form action="add.php" method="post" name="entry" onSubmit="return VerifyDataEntry(this)">

		<table>

			<tbody>

				<tr><td>Store Name<img src="images/requiredstar.gif" /></td><td><input name="storename" size="20" type="text" value="<?php if(isset($Storename)){print stripslashes($Storename);}else{print "";} ?>" /></td></tr>

				<tr><td>Sale Person</td><td><input name="saleperson" size="20" type="text" value="<?php if(isset($Saleperson)){print stripslashes($Saleperson);}else{print "";} ?>" /></td></tr>

				<tr><td>Brand<img src="images/requiredstar.gif" /></td><td><input name="brand" size="20" type="text" /></td></tr>

				<tr><td>Model<img src="images/requiredstar.gif" /></td><td><input name="model" size="20" type="text" /></td></tr>

				<tr><td>Serial Number<img src="images/requiredstar.gif" /></td><td><input name="serialnumber" id="serialnumber" size="20" type="text" /> <span id="msgbox" style="display:none"></span></td></tr>

				<tr><td>Usage<img src="images/requiredstar.gif" /></td>	<td><select name="usage"><option <?php if(isset($Usage) && $Usage == "domestic"){print "selected=\"selected\"";} ?> value="domestic">Domestic</option><option <?php if(isset($Usage) && $Usage == "commercial"){print "selected=\"selected\"";} ?> value="commercial">Commercial</option><option <?php if(isset($Usage) && $Usage == "farmer"){print "selected=\"selected\"";} ?> value="farmer">Farmer</option></select></td></tr>

                <tr><td>Purchase Date<img src="images/requiredstar.gif" /></td><td><input name="date" size="20" type="date" value="<?php if(isset($Purchasedate)){print stripslashes($Purchasedate);}else{print "";} ?>" /></td></tr>

                <tr><td>Invoice Number<img src="images/requiredstar.gif" /></td><td><input name="invoicenumber" size="20" type="text" value="<?php if(isset($Invoicenumber)){print stripslashes($Invoicenumber);}else{print "";} ?>"/>  <span id="msgbox" style="display:none"></span></td></tr>

                <tr><td>Purchase Price<img src="images/requiredstar.gif" /></td><td><input name="price" size="20" type="text" /></td></tr>

                <tr><td>Purchaser First Name<img src="images/requiredstar.gif" /></td><td><input name="firstname" size="20" type="text" value="<?php if(isset($FirstName)){print stripslashes($FirstName);}else{print "";} ?>" /></td></tr>

                <tr><td>Purchaser Last Name<img src="images/requiredstar.gif" /></td><td><input name="surname" size="20" type="text" value="<?php if(isset($Surname)){print stripslashes($Surname);}else{print "";} ?>" /></td></tr>

                <tr><td>Purchaser Contractor Number</td><td><input name="contractornumber" size="20" type="text" value="<?php if(isset($Contractornumber)){print stripslashes($Contractornumber);}else{print "";} ?>" /></td></tr>

                <tr><td>Purchaser Contractor Type</td><td><select name="contractortype"><option <?php if(isset($Contractortype) && $Contractortype == ""){print "selected=\"selected\"";} ?> value=""></option><option <?php if(isset($Contractortype) && $Contractortype == "Jim's"){print "selected=\"selected\"";} ?> value="Jim's">Jim's</option><option <?php if(isset($Contractortype) && $Contractortype == "VIP"){print "selected=\"selected\"";} ?> value="VIP">VIP</option><option <?php if(isset($Contractortype) && $Contractortype == "Procurement Aust"){print "selected=\"selected\"";} ?> value="Procurement Aust">Procurement Aust</option><option <?php if(isset($Contractortype) && $Contractortype == "Other"){print "selected=\"selected\"";} ?> value="Other">Other</option></select></td></tr>

                <tr><td>Purchaser Business Name</td><td><input name="businessname" size="20" type="text" value="<?php if(isset($Businessname)){print stripslashes($Businessname);}else{print "";} ?>" /></td></tr>

                <tr><td>Purchaser Address<img src="images/requiredstar.gif" /></td><td><input name="address" size="20" type="text" value="<?php if(isset($Address)){print stripslashes($Address);}else{print "";} ?>"  /></td></tr>

                <tr><td>Purchaser Suburb<img src="images/requiredstar.gif" /></td><td><input name="suburb" size="20" type="text" value="<?php if(isset($Suburb)){print stripslashes($Suburb);}else{print "";} ?>" /></td></tr>

                <tr><td>Purchaser Postcode<img src="images/requiredstar.gif" /></td><td><input name="postcode" size="4" type="text" value="<?php if(isset($Postcode)){print stripslashes($Postcode);}else{print "";} ?>" /></td></tr>

                <tr><td>Purchaser State<img src="images/requiredstar.gif" /></td><td><select name="state"><option <?php if(isset($State) && $State == "VIC"){print "selected=\"selected\"";} ?> value="VIC">VIC</option><option <?php if(isset($State) && $State == "NSW"){print "selected=\"selected\"";} ?> value="NSW">NSW</option><option <?php if(isset($State) && $State == "WA"){print "selected=\"selected\"";} ?> value="WA">WA</option><option <?php if(isset($State) && $State == "SA"){print "selected=\"selected\"";} ?> value="SA">SA</option><option <?php if(isset($State) && $State == "ACT"){print "selected=\"selected\"";} ?> value="ACT">ACT</option><option <?php if(isset($State) && $State == "TAS"){print "selected=\"selected\"";} ?> value="TAS">TAS</option><option <?php if(isset($State) && $State == "NT"){print "selected=\"selected\"";} ?> value="NT">NT</option><option <?php if(isset($State) && $State == "QLD"){print "selected=\"selected\"";} ?> value="QLD">QLD</option></select></td></tr>

         		<tr><td>Purchaser Phone<img src="images/requiredstar.gif" /></td><td><input name="phone" size="10" type="text" value="<?php if(isset($Phone)){print stripslashes($Phone);}else{print "";} ?>"  /></td></tr>

                <tr><td>Purchaser Email<img src="images/requiredstar.gif" /></td><td><input name="email" size="20" type="text" value="<?php if(isset($Email)){print stripslashes($Email);}else{print "";} ?>"  /></td></tr>

				<tr><td><input type="submit" value="Submit" /></td><td><input type="submit" value="Submit and Add Another" name="addanother" ></td></tr>

			</tbody>

		</table>

</form>

</div>

</div>

</div>

<div class="horizontalbreak">&nbsp;</div>

<div id="bottom">

<br />

<br />

<div class="image" style="padding-bottom:55px;">

<a href="http://allpowermarine.com.au/"><img src="images/footer_logo.png" style="padding-left:30px;" border="0" /></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="images/bottom_buttons_all.png" border="0" usemap="#Map2" />

<map name="Map2" id="Map2">

  <area shape="poly" coords="2,4,141,4,142,53,2,49" href="http://allpowershop.com.au/shop/help.php?section=contactus&mode=update" alt="Contact Us" />

  <area shape="poly" coords="155,4,297,4,297,54,159,48" href="http://allpowershop.com.au/shop/pages.php?pageid=3" alt="Terms &amp; Conditions" />

  <area shape="poly" coords="312,4,452,4,450,53,313,49" href="http://allpowershop.com.au/shop/pages.php?pageid=4" alt="Privacy Statement" />

  <area shape="poly" coords="466,4,606,4,605,54,468,48" href="http://allpowershop.com.au/shop/pages.php?pageid=2" alt="About Allpower" />

  <area shape="poly" coords="622,4,760,4,759,52,622,47" href="http://dealer.allpower.com.au/Allpower-DealerExtranet.asp?active_page_id=126" alt="Dealer Xtranet" />

  <area shape="poly" coords="777,4,914,4,913,55,779,46" href="http://allpowershop.com.au/shop/login.php" alt="Contractor Login" />

</map>

</div>

<div id="copyright">&nbsp;

<!--<div id="bottomimage">

<a href="http://allpowershop.com.au/shop/help.php?section=contactus&mode=update"><img src="images/aldi_image.gif" width="250" /></a>

</div>-->

<div id="bottomtext">

<br />

<br />

<br />

<a href="http://allpowershop.com.au/shop/sitemap.php">Sitemap</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://allpowershop.com.au/shop/pages.php?pageid=2">Allpower History</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Copyright &copy; 2010 - 2011 Allpower Industries

</div>

</div>

</div>

</div>

</div>

</body>

</html>

<?php

}

?>